package dao;

import entity.Teacher;
import util.Condition;
import util.JdbcHelper;

import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

public class TeacherService {
    private static TeacherService instance = new TeacherService();

    private TeacherService() {
    }

    public static TeacherService getInstance() {
        return instance;
    }

    public Collection<Teacher> findAll(List<Condition> conditionList) throws SQLException {
        java.sql.Connection connection = JdbcHelper.getConn();
        Collection<Teacher> teachers = TeacherDao.getInstance().findAll(connection, conditionList);
        JdbcHelper.close(connection);
        return teachers;
    }

    // ID 查询
    public Teacher findById(Long id) throws SQLException {
        java.sql.Connection connection = JdbcHelper.getConn();
        Teacher teacher = TeacherDao.getInstance().findById(connection, id);
        JdbcHelper.close(connection);
        return teacher;
    }

    // 增
    public int add(Teacher teacher) throws SQLException {
        java.sql.Connection connection = JdbcHelper.getConn();
        int rows = TeacherDao.getInstance().add(connection, teacher);
        JdbcHelper.close(connection);
        return rows;
    }

    // 改
    public int update(Teacher teacher) throws SQLException {
        java.sql.Connection connection = JdbcHelper.getConn();
        int rows = TeacherDao.getInstance().update(connection, teacher);
        JdbcHelper.close(connection);
        return rows;
    }

    // 删
    public int delete(Long id) throws SQLException {
        java.sql.Connection connection = JdbcHelper.getConn();
        int rows = TeacherDao.getInstance().delete(connection, id);
        JdbcHelper.close(connection);
        return rows;
    }
}